//
//  ViewTwo.m
//  Quartz
//
//  Created by thinkjoy on 2017/6/29.
//  Copyright © 2017年 杜瑞胜. All rights reserved.
//

#import "ViewTwo.h"
#import "NSDate+RSTool.h"

@implementation ViewTwo

- (void) drawRect: (CGRect) rect {
    
//    CGContextRef con = UIGraphicsGetCurrentContext();
//    
//    CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100));
//    
//    CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
//    
//    CGContextFillPath(con);

//    UIBezierPath    * p =   [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,0,100,100) byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(100, 400)];
//    [[UIColor whiteColor] setFill];
//    [p fill];
    
    CGFloat view_W  =   2/2.2 * MIN(CGRectGetWidth(rect), CGRectGetHeight(rect));
    CGFloat view_H  =   view_W;
    CGFloat view_X  =   (CGRectGetWidth(rect)-view_W)/2.f;
    CGFloat view_Y  =   (CGRectGetHeight(rect)-view_H)/2.f;
    CGFloat view_C_X    =   CGRectGetWidth(rect)/2;
    CGFloat view_C_Y    =   CGRectGetHeight(rect)/2;
    
//    [[UIColor greenColor] set];
//    UIBezierPath * p    =   [UIBezierPath bezierPathWithRect:CGRectMake(view_C_X-20, view_C_Y-20, 40, 40)];
//    [p stroke];
    
    //段落格式
    NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    textStyle.lineBreakMode = NSLineBreakByWordWrapping;
    textStyle.alignment = NSTextAlignmentCenter;//水平居中
    //字体
    UIFont  *font = [UIFont systemFontOfSize:12*(view_W/220.f)];
    
    [[[NSDate date] toStrWithFormartStr:@"HH:mm:ss"] drawInRect:CGRectMake(view_C_X-30*(view_W/220.f), view_C_Y-10*(view_W/220.f), 60*(view_W/220.f), 20*(view_W/220.f)) withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:[UIColor redColor],NSParagraphStyleAttributeName:textStyle}];
    
    CGFloat secondLong  =   view_W/2.f-10*(view_W/220.f)*(1+0.618);
    CGFloat minuteLong  =   secondLong*0.618;
    CGFloat hourLong    =   minuteLong*0.618;
    
    NSInteger second    =   [[[NSDate date] toStrWithFormartStr:@"ss"] integerValue];
    NSInteger minute    =   [[[NSDate date] toStrWithFormartStr:@"mm"] integerValue];
    NSInteger hour      =   [[[NSDate date] toStrWithFormartStr:@"hh"] integerValue];
    
    [[UIColor redColor] set];
    UIBezierPath * second_P    =   [UIBezierPath bezierPath];
    [second_P moveToPoint:CGPointMake(view_C_X, view_C_Y)];
    CGFloat secondAngle =   -M_PI_2+second*(2*M_PI/60.f);
    CGPoint secondPoint    =   CGPointMake(view_C_X+secondLong*cos(secondAngle), view_C_Y+secondLong*sin(secondAngle));
    [second_P addLineToPoint:secondPoint];
    [second_P stroke];
    
    
    [[UIColor redColor] set];
    UIBezierPath * minute_P    =   [UIBezierPath bezierPath];
    [minute_P moveToPoint:CGPointMake(view_C_X, view_C_Y)];
    CGFloat minuteAngle =   -M_PI_2+(minute+second/60.f)*(2*M_PI/60.f);
    CGPoint minutePoint    =   CGPointMake(view_C_X+minuteLong*cos(minuteAngle), view_C_Y+minuteLong*sin(minuteAngle));
    [minute_P addLineToPoint:minutePoint];
    [minute_P setLineWidth:2.f];
    [minute_P stroke];
    
    [[UIColor redColor] set];
    UIBezierPath * hour_P    =   [UIBezierPath bezierPath];
    [hour_P moveToPoint:CGPointMake(view_C_X, view_C_Y)];
    CGFloat hourAngle =   -M_PI_2+(hour+minute/60.f)*(2*M_PI/12.f);
    CGPoint hourPoint    =   CGPointMake(view_C_X+hourLong*cos(hourAngle), view_C_Y+hourLong*sin(hourAngle));
    [hour_P addLineToPoint:hourPoint];
    [hour_P setLineWidth:3.f];
    [hour_P stroke];
}

@end
